package com.atguigu.day03;

import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.api.functions.source.RichParallelSourceFunction;

// 自定义并行数据源
// +--------------+    +---------------+
// |      run     |    |      run      |
// +--------------+    +---------------+
//       \                      /
//        \                    /
//         v                  v
//        +-------------------+
//        |       print       |
//        +-------------------+
public class Example10 {
    public static void main(String[] args) throws Exception {
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();

        env
                .addSource(new RichParallelSourceFunction<Integer>() {
                    // 有两个并行子任务执行run函数
                    @Override
                    public void run(SourceContext<Integer> ctx) throws Exception {
                        for (int i = 1; i < 9; i++) {
                            ctx.collect(i);
                        }
                    }

                    @Override
                    public void cancel() {

                    }
                })
                .setParallelism(2)
                .print()
                .setParallelism(1);


        env.execute();
    }
}
